<template>
	<!-- 问题五参照 录入采购池，这个是录入采购池，参照的 -->
	<a-card :bordered="false">
		<div class="table-page-search-wrapper">
			<a-form layout="inline">
				<a-row :gutter="48">
					<!-- 22--{{isThree}} -->
					<a-col :md="7" :sm="24">
						<a-form-item label="物料名称">
							<a-input v-model="queryParam.materialName" placeholder="请输入物料名称" />
						</a-form-item>
					</a-col>

					<a-col :md="7" :sm="24">
						<a-form-item label="料号">
							<a-input v-model="queryParam.materialCode" placeholder="请输入料号" />
						</a-form-item>
					</a-col>

					<a-col :md="24" :sm="24"
						v-if="(isOne() || materialType == $businessEnum.MATERIAL_TYPE_ENUM.material.id || (hasRoleNoAdmin(['caiwu']) && materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id)) || materialType == $businessEnum.MATERIAL_TYPE_ENUM.customized.id">
						<a-form-item label="供应商：">
							<FCOrgRadioGroup1 v-model="queryParam.orgIdSupplier" :filter-types="materialType" />
						</a-form-item>
					</a-col>
					<!-- <a-col :md="24" :sm="24"
						v-if="(isOne() || materialType == $businessEnum.MATERIAL_TYPE_ENUM.material.id || (hasRoleNoAdmin(['caiwu']) && materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id)) || materialType == $businessEnum.MATERIAL_TYPE_ENUM.customized.id">
						<a-form-item label="供应商：">
							<FCOrgRadioGroup v-model="queryParam.orgIdSupplier"
								:filter-types="[$businessEnum.ORG_TYPE_ENUM.NORMAL_SUPPLIER.id, $businessEnum.ORG_TYPE_ENUM.PRIMARY_SUPPLIER.id]" />
						</a-form-item>
					</a-col> -->
					<a-col :md="24" :sm="24" v-if="isFour() && materialType == '1473944715475824641'">
						<a-form-item label="归属公司：">
							<FCOrgRadioGroup v-model="queryParam.orgIdCompany"
								:filter-types="[$businessEnum.ORG_TYPE_ENUM.COMPANY.id]" />
						</a-form-item>
					</a-col>
					<a-col :md="7" :sm="24"
						v-if="hasRoleNoAdmin(['general manager', 'caiwu', 'material_admin1', 'material_admin2', 'material_admin3', 'material_admin4'])">
						<a-form-item label="导出时间选择">
							<a-range-picker style="width:100%" v-model="queryParam.timeList" format="YYYY-MM-DD" @change="onChange" />
						</a-form-item>
					</a-col>
					<a-col :md="6" :sm="24">
						<a-form-item label="领用方">
							<a-select placeholder="全部" v-model="queryParam.classGroupId">
								<!-- <a-select-option :value="null">
									全部
								</a-select-option> -->
								<a-select-option :value="item.classGroupId" v-for="(item, index) in teamList" :key="index">
									{{ item.name }}
								</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
					<a-col :md="8" :sm="24" v-if="$businessEnum.MATERIAL_TYPE_ENUM.packing.id === materialType">
						<a-form-item label="尺寸">
							<a-input v-model="queryParam.extend1" placeholder="请输入尺寸" />
						</a-form-item>
					</a-col>
					<a-col :md="8" :sm="24" v-if="$businessEnum.MATERIAL_TYPE_ENUM.packing.id === materialType">
						<a-form-item label="库位">
							<a-cascader :options="storehouseList" expand-trigger="hover" @change="storehouseNameChange"
								v-model="orgIdStorehousePositionName" placeholder="请输入" />
						</a-form-item>
					</a-col>
					<a-col :md="!advanced && 3 || 24" :sm="24">
						<span class="table-page-search-submitButtons"
							:style="advanced && { float: 'right', overflow: 'hidden' } || {}">
							<a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
							<a-button style="margin-left: 8px"
								@click="() => { queryParam = { orgIdSupplier: null, orgIdCompany: null, timeList: [], classGroupId: undefined, orgIdStorehousePositionName: null }; $refs.table.refresh(true) }">重置</a-button>
						</span>
					</a-col>
				</a-row>
			</a-form>
		</div>
		<!---->
		<div class="table-operator">
			<span
				v-if="isFour() || (hasRoleNoAdmin(['caiwu']) && materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id) || (isJSYOrSCWY() && materialType == $businessEnum.MATERIAL_TYPE_ENUM.customized.id)">
				<a-button type="primary"
					v-if="(isCaigou() && (materialType == $businessEnum.MATERIAL_TYPE_ENUM.packing.id || materialType == $businessEnum.MATERIAL_TYPE_ENUM.material.id)) || !isCaigou()"
					@click="$refs.addForm.add({ materialType })">新增</a-button>
				<a-button type="primary"
					@click="$refs.toPoolForm.add({ materialType, poolType: $businessEnum.ORDER_TYPE_ENUM.purchaseOrder.id, selectedRows })">录入采购池
				</a-button>
				<a-button type="primary"
					@click="$refs.toPoolForm.add({ materialType, poolType: $businessEnum.ORDER_TYPE_ENUM.inOrder.id, selectedRows })">无单入库
				</a-button>
				<a-button type="primary"
					@click="$refs.toPoolForm.add({ materialType, poolType: $businessEnum.ORDER_TYPE_ENUM.outOrder.id, selectedRows })">无单出库
				</a-button>
			</span>
			<a-button type="primary" v-if="isJSYRole() && materialType == $businessEnum.MATERIAL_TYPE_ENUM.material.id"
				@click="$refs.toPoolForm.add({ materialType, poolType: $businessEnum.ORDER_TYPE_ENUM.purchaseOrder.id, selectedRows })">录入采购池
			</a-button>
			<span>
				<Export
					v-if="hasRoleNoAdmin(['general manager', 'caiwu']) || (isJSYRole() && materialType == $businessEnum.MATERIAL_TYPE_ENUM.customized.id)"
					text="盘点导出" title="物料盘点统计表" url="/emsMaterial/inventoryExport" :obj="{
						materialType: materialType,
						isMainSupplier: queryParam.isMainSupplier,
						materialCode: queryParam.materialCode,
						materialName: queryParam.materialName,
						orgIdSupplier: queryParam.orgIdSupplier,
						startTime: appendTime(queryParam.timeList)[0],
						endTime: appendTime(queryParam.timeList)[1],
					}" />
				<!-- 结存导出 -->
				<span>
					<!-- 原料 -->
					<span v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.material.id">
						<!-- 显示金额 -->
						<Export v-if="hasBtnPremission(['emsMaterial:export:material'])" text="结存导出" title="结存物料表"
							url="/emsMaterial/export" :obj="{
								materialType: materialType,
								isMainSupplier: queryParam.isMainSupplier,
								materialCode: queryParam.materialCode,
								materialName: queryParam.materialName,
								orgIdSupplier: queryParam.orgIdSupplier,
								startTime: appendTime(queryParam.timeList)[0],
								endTime: appendTime(queryParam.timeList)[1],
							}" />
						<!-- 不显示金额 -->
						<Export v-if="hasBtnPremission(['emsMaterial:export:material:roleType'])" text="结存导出" title="结存物料表"
							url="/emsMaterial/export" :obj="{
								materialType: materialType,
								isMainSupplier: queryParam.isMainSupplier,
								materialCode: queryParam.materialCode,
								materialName: queryParam.materialName,
								orgIdSupplier: queryParam.orgIdSupplier,
								roleType: 3,
								startTime: appendTime(queryParam.timeList)[0],
								endTime: appendTime(queryParam.timeList)[1],
							}" />
					</span>
					<!-- 定制料 -->
					<span v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.customized.id">
						<!-- 显示金额 -->
						<Export v-if="hasBtnPremission(['emsMaterial:export:customized'])" text="结存导出" title="结存物料表"
							url="/emsMaterial/export" :obj="{
								materialType: materialType,
								isMainSupplier: queryParam.isMainSupplier,
								materialCode: queryParam.materialCode,
								materialName: queryParam.materialName,
								orgIdSupplier: queryParam.orgIdSupplier,
								startTime: appendTime(queryParam.timeList)[0],
								endTime: appendTime(queryParam.timeList)[1],
							}" />
						<!-- 不显示金额 -->
						<Export v-if="hasBtnPremission(['emsMaterial:export:customized:roleType'])" text="结存导出" title="结存物料表"
							url="/emsMaterial/export" :obj="{
								materialType: materialType,
								isMainSupplier: queryParam.isMainSupplier,
								materialCode: queryParam.materialCode,
								materialName: queryParam.materialName,
								orgIdSupplier: queryParam.orgIdSupplier,
								roleType: 3,
								startTime: appendTime(queryParam.timeList)[0],
								endTime: appendTime(queryParam.timeList)[1],
							}" />
					</span>
					<!-- 包材 -->
					<span v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.packing.id">
						<!-- 显示金额 -->
						<Export v-if="hasBtnPremission(['emsMaterial:export:packing'])" text="结存导出" title="结存物料表"
							url="/emsMaterial/export" :obj="{
								materialType: materialType,
								isMainSupplier: queryParam.isMainSupplier,
								materialCode: queryParam.materialCode,
								materialName: queryParam.materialName,
								orgIdSupplier: queryParam.orgIdSupplier,
								startTime: appendTime(queryParam.timeList)[0],
								endTime: appendTime(queryParam.timeList)[1],
							}" />
						<!-- 不显示金额 -->
						<Export v-if="hasBtnPremission(['emsMaterial:export:packing:roleType'])" text="结存导出" title="结存物料表"
							url="/emsMaterial/export" :obj="{
								materialType: materialType,
								isMainSupplier: queryParam.isMainSupplier,
								materialCode: queryParam.materialCode,
								materialName: queryParam.materialName,
								orgIdSupplier: queryParam.orgIdSupplier,
								roleType: 3,
								startTime: appendTime(queryParam.timeList)[0],
								endTime: appendTime(queryParam.timeList)[1],
							}" />
					</span>
					<!-- 线缆 -->
					<span v-if="materialType == $businessEnum.MATERIAL_TYPE_ENUM.cable.id">
						<!-- 显示金额 -->
						<Export v-if="hasBtnPremission(['emsMaterial:export:cable'])" text="结存导出" title="结存物料表"
							url="/emsMaterial/export" :obj="{
								materialType: materialType,
								isMainSupplier: queryParam.isMainSupplier,
								materialCode: queryParam.materialCode,
								materialName: queryParam.materialName,
								orgIdSupplier: queryParam.orgIdSupplier,
								startTime: appendTime(queryParam.timeList)[0],
								endTime: appendTime(queryParam.timeList)[1],
							}" />
						<!-- 不显示金额 -->
						<Export v-if="hasBtnPremission(['emsMaterial:export:cable:roleType'])" text="结存导出" title="结存物料表"
							url="/emsMaterial/export" :obj="{
								materialType: materialType,
								isMainSupplier: queryParam.isMainSupplier,
								materialCode: queryParam.materialCode,
								materialName: queryParam.materialName,
								orgIdSupplier: queryParam.orgIdSupplier,
								roleType: 3,
								startTime: appendTime(queryParam.timeList)[0],
								endTime: appendTime(queryParam.timeList)[1],
							}" />
					</span>
					<!-- <Export
					v-if="hasRoleNoAdmin(['general manager', 'caiwu']) || (isJSYRole() && materialType == $businessEnum.MATERIAL_TYPE_ENUM.customized.id)"
					text="结存导出" title="结存物料表" url="/emsMaterial/export" :obj="{
						materialType: materialType,
						isMainSupplier: queryParam.isMainSupplier,
						materialCode: queryParam.materialCode,
						materialName: queryParam.materialName,
						orgIdSupplier: queryParam.orgIdSupplier,
						startTime: appendTime(queryParam.timeList)[0],
						endTime: appendTime(queryParam.timeList)[1],
					}" /> -->
					<!-- 原料管理员单独的按钮 传roleType = 3 -->
					<!-- <Export v-if="hasRoleNoAdmin(['material_admin1'])" text="结存导出" title="结存物料表" url="/emsMaterial/export" :obj="{
					materialType: materialType,
					isMainSupplier: queryParam.isMainSupplier,
					materialCode: queryParam.materialCode,
					materialName: queryParam.materialName,
					orgIdSupplier: queryParam.orgIdSupplier,
					roleType: 3,
					startTime: appendTime(queryParam.timeList)[0],
					endTime: appendTime(queryParam.timeList)[1],
				}" /> -->
					<!-- 生产文员 结存导出按钮 不显示价格 -->
					<!-- <Export
					v-if="hasRoleNoAdmin(['Production clerk']) && materialType == $businessEnum.MATERIAL_TYPE_ENUM.customized.id"
					text="结存导出" title="结存物料表" url="/emsMaterial/export" :obj="{
						materialType: materialType,
						isMainSupplier: queryParam.isMainSupplier,
						materialCode: queryParam.materialCode,
						materialName: queryParam.materialName,
						orgIdSupplier: queryParam.orgIdSupplier,
						roleType: 3,
						startTime: appendTime(queryParam.timeList)[0],
						endTime: appendTime(queryParam.timeList)[1],
					}" /> -->
				</span>
				<Export
					v-if="hasRoleNoAdmin(['general manager', 'caiwu']) || (isJSYRole() && materialType == $businessEnum.MATERIAL_TYPE_ENUM.customized.id)"
					text="下载价格/供应商更新导入模板" title="价格更新导入模板" url="/emsMaterial/priceUpdateImportTemplate" :obj="{
						materialType: materialType,
					}" />
				<Import
					v-if="hasRoleNoAdmin(['general manager', 'caiwu']) || (isJSYRole() && materialType == $businessEnum.MATERIAL_TYPE_ENUM.customized.id)"
					type="danger" text="价格/供应商更新导入" url="/emsMaterial/priceUpdateImExport" @ok="$refs.table.refresh()" />
			</span>

			<Export text="下载领用方更新导入模板" v-if="hasPerm('emsMaterial:classGroupUpdateImportTemplate')" title="领用方更新导入模板"
				url="/emsMaterial/classGroupUpdateImportTemplate" :obj="{
					materialType: materialType,
				}" />
			<Import type="danger" v-if="hasPerm('emsMaterial:classGroupUpdateImExport')" style="margin: 10px 0px;"
				text="领用方更新导入" url="/emsMaterial/classGroupUpdateImExport" @ok="$refs.table.refresh()" />
		</div>

		<s-table ref="table" :scroll="{ x: 1400, y: 1000 }" :customRow="customRow" style="max-height: 500px;" size="default"
			:columns="columnsDynamic" :data="loadData" :alert="true" :rowKey="(record) => record.materialId"
			:rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }">


			<template slot="inventoryNumber" slot-scope="text, record">
				<span :class="record.inventoryNumber < record.inventoryWarningValue ? 'low-value' : ''">{{ text }}</span>
			</template>

			<span slot="image" slot-scope="text,record">
				<image-preview1 :images="text" :imageUrl="record.imageUrl" />
			</span>

			<span slot="actionMaterial" slot-scope="text, record">
				<a @click="$refs.editForm.edit(record, { 'readonly': true })">详情</a>

				<template
					v-if="isFour() || hasRoleNoAdmin(['caiwu']) || ((isJSYRole() || isSCWY()))">
					<a-divider v-if="!isCaigou()" type="vertical" />
					<a v-if="!isCaigou()" @click="$refs.editForm.edit(record)">编辑</a>
				</template>
				<a-divider type="vertical" />
				<a @click="$refs.useChart.show({ ...record, typeId: 1 })">出入库情况</a>
				<a-divider type="vertical"
					v-if="hasRoleNoAdmin(['general manager', 'caiwu']) || (isJSYRole())" />
				<a @click="$refs.floatChart.show({ ...record })"
					v-if="hasRoleNoAdmin(['general manager', 'caiwu']) || (isJSYRole())">浮动情况</a>
				<!--        <a-divider type="vertical"/>-->
				<!--        <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">-->
				<!--          <a>删除</a>-->
				<!--        </a-popconfirm>-->
			</span>

			<span slot="actionProduct" slot-scope="text, record">
				<a @click="$refs.editForm.edit(record)">详情</a>
				<template v-if="isFour()">
					<a-divider type="vertical" />
					<a @click="$refs.editForm.edit(record)">编辑</a>
				</template>
			</span>
		</s-table>
		<add-form ref="addForm" @ok="handleOk" :materialType="materialType" />
		<edit-form ref="editForm" @ok="handleOk" :materialType="materialType" />
		<to-pool-form ref="toPoolForm" @ok="handleOk" />
		<use-chart ref="useChart" />
		<float-chart ref="floatChart" />

	</a-card>
</template>

<script>
import { STable } from '@/components'
import moment from 'moment'
import { del, page } from '@/api/modular/main/emsmaterial/emsMaterialManage'
import addForm from './emsMaterialAddForm'
import editForm from './emsMaterialEditForm'
import toPoolForm from './emsMaterialToPoolForm1'
import materialUsageStatistics from './emsMaterialUsageStatistics'
import FCOrgRadioGroup from '@/components/FcOrgRadioGroup/FCOrgRadioGroup'
import FCOrgRadioGroup1 from '@/components/FcOrgRadioGroup/FCOrgRadioGroup1'
import useChart from '@/views/main/materialManagement/materialBaseTable/useChart'
import floatChart from './floatChart'
// import ImagePreview from '@/components/pbUpload/ImagePreview.vue'

import { page as teamPage } from '@/api/modular/main/classGroup/classGroup'

import { list as emsOrgList } from '@/api/modular/main/emsorg/emsOrgManage'

export default {
	components: {
		FCOrgRadioGroup,
		FCOrgRadioGroup1,
		STable,
		addForm,
		editForm,
		toPoolForm,
		materialUsageStatistics,
		useChart,
		floatChart,
		// ImagePreview
	},
	mounted() {
		// 生成默认时间
		// this.queryParam.timeList = this.getDates()
		this.$set(this.queryParam, 'timeList', this.getDates())
		console.log('@@@', this.getDates())
	},
	data() {
		return {
			advanced: false, // 高级搜索 展开/关闭
			queryParam: {
				classGroupId: undefined
			},  // 查询参数
			// 加载数据方法 必须为 Promise 对象
			loadData: parameter => {
				// this.queryParam.materialType = this.materialType
				// 如果是原料仓库
				if (0) {
					// 如果是采购
					if (this.isCaigou()) {
						this.queryParam = {
							...this.queryParam,
							materialType: this.materialType,
							isMainSupplier: 2,  //是采购
						}
					} else {
						this.queryParam = {
							...this.queryParam,
							materialType: this.materialType,
							isMainSupplier: 1,  //不是采购
						}
					}
				}
				//如果不是原料仓库
				else {
					this.queryParam = {
						...this.queryParam,
						materialType: this.materialType,
						// isMainSupplier: 1  //如果不是原料仓库，不传这个字段
					}
				}
				delete this.queryParam.timeList

				return page(Object.assign(parameter, this.queryParam)).then((res) => {
					return res.data
				})
			},
			selectedRowKeys: [],
			selectedRows: [],
			defaultExpandedKeys: [],
			replaceFields: {
				key: 'id',
			},
			previewImage: '',
			orgList: [],
			suppliers: [],
			orgs: [],
			viewState: '',
			teamList: [],
			sysOrgs: [],
			orgIdStorehousePositionName: null,
		}
	},
	computed: {
		materialType() {
			let viewState = this.routePath.viewState
			return this.$businessEnum.MATERIAL_TYPE_ENUM[viewState].id
		},
		routePath() {
			let urlParam = this.$util.getUrlParam(this.$route.path)
			return urlParam
		},
		/**
		 * 仓库
		 * @returns {{label: string, value: string}[]|*[]}
		 */
		storehouseList() {
			if (this.sysOrgs.length == 0) {
				return []
			}
			let arr = this.sysOrgs.filter((org) => org.unitRanId == this.$businessEnum.ORG_TYPE_ENUM.STOREHOUSE.id && org.materialType == this.materialType).map((org) => {
				return {
					value: org.id, label: org.name,
				}
			})
			const orgTypeId = this.$businessEnum.ORG_TYPE_ENUM.STOREHOUSE_POSITION.id
			arr.forEach(val => {
				const storehouseId = val.value
				let children = this.sysOrgs.filter((org) => {
					return org.unitRanId == orgTypeId && org.pid == storehouseId
				}).map((org) => {
					return { value: org.id, label: org.name }
				})
				val.children = children;
			})
			return arr;
		},

		columnsDynamic() {
			let viewState = this.routePath.viewState
			let result
			if (viewState == this.$businessEnum.MATERIAL_TYPE_ENUM.material.code) {
				// 原料基础库
				result = [
					{
						title: '序号', width: '80px',
						align: 'center',
						customRender: (text, record, index) => `${index + 1}`,
					},
					{
						title: '料号',
						dataIndex: 'materialCode'
					},
					{
						title: '物料名称',
						dataIndex: 'materialName'
					},
					{
						title: '规格',
						dataIndex: 'materialModelNumber'
					},
					{
						title: '仓库',
						dataIndex: 'orgIdStorehouseName'
					},
					{
						title: '成本价(不含税)',
						dataIndex: 'costPrice'
					},
					{
						title: '采购价(含税)',
						dataIndex: 'purchasePrice',
						customRender: (text, record, index) => {
							return text || text == 0 ? text : '--'
						},
					},
					{
						title: '库位',
						dataIndex: 'orgIdStorehousePositionName'
					},
					{
						title: '图片',
						dataIndex: 'image',
						// customRender: (text, record, index) => this.$util.createFcImage(this.$createElement, text)
						scopedSlots: { customRender: 'image' }
					},
					{
						title: '库存',
						dataIndex: 'inventoryNumber',
						scopedSlots: { customRender: 'inventoryNumber' }
					},
					{
						title: '预警值',
						dataIndex: 'inventoryWarningValue'
					},
					{
						title: '领用方',
						dataIndex: 'classGroupName'
					},
					{
						title: '归属公司',
						dataIndex: 'orgIdCompanyName',
						ellipsis: false,
					},
					{
						title: '供应商',
						dataIndex: 'orgIdSupplierName',
						ellipsis: false,
					},
					{
						title: '排序',
						dataIndex: 'sort',
						ellipsis: false,
					},
					{
						title: '操作',
						width: '300px',
						dataIndex: 'actionMaterial',
						fixed: 'right',
						scopedSlots: { customRender: 'actionMaterial' }
					}
				]
			} else if (viewState == this.$businessEnum.MATERIAL_TYPE_ENUM.customized.code) {
				// 定制料基础库
				result = [
					{
						title: '序号', width: '80px',
						align: 'center',
						customRender: (text, record, index) => `${index + 1}`,
					},
					{
						title: '料号',
						dataIndex: 'materialCode'
					},
					{
						title: '物料名称',
						dataIndex: 'materialName'
					},
					{
						title: '规格',
						dataIndex: 'materialModelNumber'
					},
					{
						title: '仓库',
						dataIndex: 'orgIdStorehouseName'
					},
					{
						title: '成本价(不含税)',
						dataIndex: 'costPrice'
					},
					{
						title: '采购价(含税)',
						dataIndex: 'purchasePrice',
						customRender: (text, record, index) => {
							return text || text == 0 ? text : '--'
						},
					},
					{
						title: '库位',
						dataIndex: 'orgIdStorehousePositionName'
					},
					{
						title: '图片',
						dataIndex: 'image',
						// customRender: (text, record, index) => this.$util.createFcImage(this.$createElement, text)
						scopedSlots: { customRender: 'image' }
					},
					{
						title: '库存',
						dataIndex: 'inventoryNumber',
						scopedSlots: { customRender: 'inventoryNumber' }
					},
					{
						title: '预警值',
						dataIndex: 'inventoryWarningValue'
					},
					{
						title: '领用方',
						dataIndex: 'classGroupName'
					},
					{
						title: '归属公司',
						dataIndex: 'orgIdCompanyName',
						ellipsis: false,
					},
					{
						title: '供应商',
						dataIndex: 'orgIdSupplierName',
						ellipsis: false,
					},
					{
						title: '排序',
						dataIndex: 'sort',
						ellipsis: false,
					},
					{
						title: '操作',
						width: '300px',
						dataIndex: 'actionMaterial',
						fixed: 'right',
						scopedSlots: { customRender: 'actionMaterial' }
					}
				]
			} else if (viewState == this.$businessEnum.MATERIAL_TYPE_ENUM.product.code) {
				// 产品基础库
				result = [
					{
						title: '序号', width: '80px',
						align: 'center',
						customRender: (text, record, index) => `${index + 1}`,
					},
					{
						title: '料号',
						dataIndex: 'materialCode'
					},
					{
						title: '产品名称',
						dataIndex: 'materialName'
					},
					{
						title: '规格',
						dataIndex: 'materialModelNumber'
					},
					{
						title: '仓库',
						dataIndex: 'orgIdStorehouseName'
					},
					{
						title: '成本价(不含税)',
						dataIndex: 'costPrice'
					},
					{
						title: '采购价(含税)',
						dataIndex: 'purchasePrice',
						customRender: (text, record, index) => {
							return text || text == 0 ? text : '--'
						},
					},
					{
						title: '库位',
						dataIndex: 'orgIdStorehousePositionName'
					},
					{
						title: '图片',
						dataIndex: 'image',
						// customRender: (text, record, index) => this.$util.createFcImage(this.$createElement, text)
						scopedSlots: { customRender: 'image' }
					},
					{
						title: '库存',
						dataIndex: 'inventoryNumber',
						scopedSlots: { customRender: 'inventoryNumber' }
					},
					{
						title: '预警值',
						dataIndex: 'inventoryWarningValue'
					},
					{
						title: '领用方',
						dataIndex: 'classGroupName'
					},
					{
						title: '归属公司',
						dataIndex: 'orgIdCompanyName',
						ellipsis: false,
					},
					{
						title: '操作',
						width: '300px',
						fixed: 'right',
						dataIndex: 'actionProduct',
						scopedSlots: { customRender: 'actionProduct' }
					}
				]
			} else if (viewState == this.$businessEnum.MATERIAL_TYPE_ENUM.packing.code) {
				// 包材基础库
				result = [
					{
						title: '序号', width: '80px',
						align: 'center',
						customRender: (text, record, index) => `${index + 1}`,
					},
					{
						title: '料号',
						dataIndex: 'materialCode'
					},
					{
						title: '名称',
						dataIndex: 'materialName'
					},
					{
						title: '规格',
						dataIndex: 'materialModelNumber'
					},
					{
						title: '尺寸',
						dataIndex: 'extend1'
					},
					{
						title: '仓库',
						dataIndex: 'orgIdStorehouseName'
					},
					{
						title: '成本价(不含税)',
						dataIndex: 'costPrice'
					},
					{
						title: '采购价(含税)',
						dataIndex: 'purchasePrice',
						customRender: (text, record, index) => {
							return text || text == 0 ? text : '--'
						},
					},
					{
						title: '库位',
						dataIndex: 'orgIdStorehousePositionName'
					},
					{
						title: '图片',
						dataIndex: 'image',
						// customRender: (text, record, index) => this.$util.createFcImage(this.$createElement, text)
						scopedSlots: { customRender: 'image' }
					},
					{
						title: '库存',
						dataIndex: 'inventoryNumber',
						scopedSlots: { customRender: 'inventoryNumber' }
					},
					{
						title: '预警值',
						dataIndex: 'inventoryWarningValue'
					},
					{
						title: '领用方',
						dataIndex: 'classGroupName'
					},
					{
						title: '供应商',
						dataIndex: 'orgIdSupplierName',
						ellipsis: false,
					},
					{
						title: '归属公司',
						dataIndex: 'orgIdCompanyName',
						ellipsis: false,
					},
					{
						title: '排序',
						dataIndex: 'sort',
						ellipsis: false,
					},
					// {
					//   title: '供应商欠货数量',
					//   dataIndex: 'owedQuantity',
					// },
					{
						title: '操作',
						width: '300px',
						fixed: 'right',
						dataIndex: 'actionMaterial',
						scopedSlots: { customRender: 'actionMaterial' }
					}
				]
			} else if (viewState == this.$businessEnum.MATERIAL_TYPE_ENUM.cable.code) {
				// 线缆基础库
				result = [
					{
						title: '序号',
						align: 'center',
						width: '100px',
						customRender: (text, record, index) => `${index + 1}`,
					},
					{
						title: '料号',
						width: '120px',
						dataIndex: 'materialCode'
					},
					{
						title: '材质',
						width: '100px',
						dataIndex: 'extend1'
					},
					{
						title: '颜色',
						width: '100px',
						dataIndex: 'extend3'
					},
					{
						title: '名称',
						width: '150px',
						dataIndex: 'materialName'
					},
					{
						title: '规格',
						width: '180px',
						dataIndex: 'materialModelNumber'
					},
					{
						title: '外径',
						width: '100px',
						dataIndex: 'extend6'
					},
					{
						title: '库存',
						width: '100px',
						dataIndex: 'inventoryNumber',
						scopedSlots: { customRender: 'inventoryNumber' }
					},
					{
						title: '成本价(不含税)',
						width: '100px',
						dataIndex: 'costPrice'
					},
					{
						title: '采购价(含税)',
						width: '100px',
						dataIndex: 'purchasePrice',
						customRender: (text, record, index) => {
							return text || text == 0 ? text : '--'
						},
					},
					{
						title: '预警值',
						width: '100px',
						dataIndex: 'inventoryWarningValue'
					},
					{
						title: '领用方',
						width: '100px',
						dataIndex: 'classGroupName'
					},
					{
						title: '周期出库数量',
						width: '100px',
						dataIndex: 'ckNumber'
					},
					{
						title: '周期入库数量',
						width: '100px',
						dataIndex: 'rkNumber'
					},
					{
						title: '仓库',
						width: '100px',
						dataIndex: 'orgIdStorehouseName'
					},
					{
						title: '库位',
						width: '100px',
						dataIndex: 'orgIdStorehousePositionName'
					},
					{
						title: '图片',
						dataIndex: 'image',
						width: '100px',
						// customRender: (text, record, index) => this.$util.createFcImage(this.$createElement, text)
						scopedSlots: { customRender: 'image' }
					},
					{
						title: '米重',
						width: '100px',
						dataIndex: 'extend5'
					},
					{
						title: '供应商',
						width: '130px',
						dataIndex: 'orgIdSupplierName',
						ellipsis: false,
					},
					{
						title: '排序',
						dataIndex: 'sort',
						width: '100px',
						ellipsis: false,
					},
					{
						title: '操作',
						dataIndex: 'actionMaterial',
						fixed: 'right',
						width: '300px',
						scopedSlots: { customRender: 'actionMaterial' }
					}
				]
			} else {
				return []
			}
			// 如果是非财务角色 && 线缆基础库，给列表删掉供应商
			// if( !(this.hasRoleNoAdmin(['caiwu']) && this.materialType==this.$businessEnum.MATERIAL_TYPE_ENUM.cable.id) ){
			//   result.splice(-2, 0)
			//   // console.log('result------------', result)
			// }
			//如果是子公司，就删掉供应商和归属公司(财务&&线缆仓库是要看到供应商的)
			if (this.isSubOrg && !(this.hasRoleNoAdmin(['caiwu']) && this.materialType == this.$businessEnum.MATERIAL_TYPE_ENUM.cable.id)) {
				let index1 = result.findIndex(value => value.title == '供应商')
				let index2 = result.findIndex(value => value.title == '归属公司')

				if (index1 > -1) {
					result.splice(index1, 1)
				}
				if (index2 > -1 && this.materialType != '1473944715475824641') {
					result.splice(index2, 1)
				}
			}
			if (this.delCompany) {
				let index2 = result.findIndex(value => value.title == '归属公司')
				if (index2 > -1 && this.materialType != '1473944715475824641') {
					result.splice(index2, 1)
				}
			}
			//总经理，财务，采购，技术员(定制料基础库) 之外，如果有成本价(不含税)，就删掉这列
			if (!this.isThree) {
				let index3 = result.findIndex(value => value.title == '成本价(不含税)')
				if (index3 > -1) {
					result.splice(index3, 1)
				}
			}
			//总经理，财务，采购，技术员(定制料基础库)  之外，如果有成本价(不含税)，就删掉这列
			if (!this.isThree) {
				let index99 = result.findIndex(value => value.title == '采购价(含税)')
				if (index99 > -1) {
					result.splice(index99, 1)
				}
			}
			//如果是四个仓库管理员，就删掉供应商（归属公司不能删）
			if (this.isFourAdmin && !(this.hasRoleNoAdmin(['caiwu']) && this.materialType == this.$businessEnum.MATERIAL_TYPE_ENUM.cable.id)) {
				let index4 = result.findIndex(value => value.title == '供应商')

				if (index4 > -1) {
					result.splice(index4, 1)
				}
			}
			return result
		},
		isSubOrg() {
			return this.hasRole(this.$util.ROLE_ENUM.GS_ADMIN.id)
				|| (this.hasRole(this.$util.ROLE_ENUM.SQCG.id) && this.materialType == this.$businessEnum.MATERIAL_TYPE_ENUM.cable.id)   //海外仓
		},
		// 删除归属公司
		delCompany() {
			return !(this.isFour() && this.materialType == '1473944715475824641')
		},

		//判断是不是 总经理，财务，采购 三者中的一种
		isThree() {
			return this.hasRole(this.$util.ROLE_ENUM.GENERALMANAGER.id)
				|| this.hasRole(this.$util.ROLE_ENUM.CAIWU.id)
				|| this.hasRole(this.$util.ROLE_ENUM.SQCG.id)
				|| (this.isJSYRole() && this.materialType == this.$businessEnum.MATERIAL_TYPE_ENUM.customized.id)
		},
		isFourAdmin() {
			return (
				this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)   //线缆
				|| this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN2.id)   //包材
				|| this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN1.id)   //原料
				|| this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN5.id)   //海外仓
			) && !this.hasRoleNoAdmin(['SQCG'])
		}
	},
	watch: {
		routePath() {
			this.$refs.table.refresh()
		}
	},
	created() {
		teamPage({
			pageSize: -1,
			status: 0
		}).then(res => {
			console.log(res);
			this.teamList = res.data.rows;
		})
		this.getSysOrgs();
	},
	methods: {
		customRow(record, index) {
			if (record.extend3) {
				return {
					style: {
						// backgroundColor: '#67C23A',
						color: record.extend3
					},
				};
			} else {
				return {
					style: {
						// backgroundColor: '#E6A23C',
						// color:"#E6A23C"
					},
				};
			}
		},
		getDates() {
			const today = new Date(); // 获取今天的日期
			const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1); // 获取本月1号的日期
			return [moment(firstDayOfMonth), moment(today)];
		},
		/**
		 * 获取仓库
		 */
		getSysOrgs() {
			emsOrgList().then((res) => {
				this.sysOrgs = res.data
			})
		},
		/**
	 * 库位
	 * @returns {{label: string, value: string}[]|*[]}
	 */
		storehouseNameChange(e) {
			console.log(e);
			this.queryParam.orgIdStorehousePosition = e[1];
		},
		del(record) {
			del(record).then((res) => {
				if (res.success) {
					this.$message.success('删除成功')
					this.$refs.table.refresh()
				} else {
					this.$message.error('删除失败：' + res.message)
				}
			}).catch((err) => {
				this.$message.error('删除错误：' + err.message)
			})
		},
		toggleAdvanced() {
			this.advanced = !this.advanced
		},
		handleOk() {
			this.$refs.table.refresh()
		},
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.selectedRows = selectedRows
		},
		onChange(e) {
			console.log('@@', e)
		},
		canReadSupplier() {
			return !this.hasRole(this.$util.ROLE_ENUM.GS_ADMIN.id)
				&& !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)
				&& !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN2.id)
		},
		canReadOrgIdCompany() {
			return !this.hasRole(this.$util.ROLE_ENUM.GS_ADMIN.id) && !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)
		},
		canReadCostPrice() {
			return !this.hasRole(this.$util.ROLE_ENUM.GS_ADMIN.id)
				&& !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)
				&& !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN2.id)
				&& !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN1.id)
		},
		//总经理    返回true
		isOne() {
			return this.hasRole(this.$util.ROLE_ENUM.GENERALMANAGER.id)  //总经理
				|| this.hasRole(this.$util.ROLE_ENUM.SQCG.id)   //采购
		},
		//四个仓库的管理员    返回true
		isThreePerson() {
			return this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)   //线缆
				|| this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN2.id)   //包材
				|| this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN1.id)   //原料
				|| this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN5.id)   //海外仓
		},
		isCaigou() {
			return this.hasRole(this.$util.ROLE_ENUM.SQCG.id)   //海外仓
		},
		//总经理，以及四个仓库的管理员    返回true
		isFour() {
			return this.hasRole(this.$util.ROLE_ENUM.GENERALMANAGER.id)  //总经理
				|| this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)   //线缆
				|| this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN2.id)   //包材
				|| this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN1.id)   //原料
				|| this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN5.id)   //海外仓
				|| this.hasRole(this.$util.ROLE_ENUM.SQCG.id)   //采购
		},

	},
}
</script>

<style lang="less">
.table-operator {
	margin-bottom: 18px;
}

button {
	margin-right: 8px;
}

.table-image {
	cursor: pointer;
	max-width: 100px;
	max-height: 50px;
	border: solid 1px #cccccc;
}

.low-value {
	color: red;
}

image {
	width: 45px;
	height: 45px;
}
</style>
